import React from 'react';
import {Table} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {Link} from 'react-router-dom';
import {PAGINATION_OPTIONS, TABLE, COMMON_TABLE_QUERY_FAIL_TEXT, COMMON_TABLE_EMPTY_TEXT, DATETIME_FORMAT} from '../constants';
import {injectIntl} from './intl';
import {conventEnumValueToString, formatDateTime, formatAmount} from '../utils';
import {partClaimStatusHis} from '../Enum';
export class HisTablePanel extends React.PureComponent {
    constructor(props) {
        super(props);
        this.onChangeSort = this.onChangeSort.bind(this);
        this.state = {
            loading: false
        };
    }
    onChangeSort = (pagination, filters, sorter) => {
        this.props.onChangeSort({
            pageIndex: pagination.current - 1,
            pageSize: pagination.pageSize,
            sortedColumnKey: sorter.columnKey,
            sortedOrder: sorter.order,
        });
    };
    render() {
        const colums = [
            {
                title: this.props.intl.formatMessage({
                    id: 'hisTablePanel.column.code',
                    defaultMessage: '编号'
                }),
                dataIndex: 'code',
                width: '100px',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'code' && this.props.sortedOrder,
                render: (text, record) => <Link to={routes.detailHis.format(record.id)} >{text}</Link>
            }, {
                title: this.props.intl.formatMessage({
                    id: 'hisTablePanel.column.status',
                    defaultMessage: '状态'
                }),
                dataIndex: 'status',
                sorter: true,
                sortOrder: this.props.sortedColumnKey === 'status' && this.props.sortedOrder,
                render: text => conventEnumValueToString(partClaimStatusHis, text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'hisTablePanel.column.claimAmount',
                    defaultMessage: '索赔金额'
                }),
                dataIndex: 'claimAmount',
                render: text => formatAmount(text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'hisTablePanel.column.approvalAmount',
                    defaultMessage: '批准金额'
                }),
                dataIndex: 'approvalAmount',
                render: text => formatAmount(text)
            }, {
                title: this.props.intl.formatMessage({
                    id: 'hisTablePanel.column.submitDate',
                    defaultMessage: '提报日期'
                }),
                dataIndex: 'submitDate',
                render: text => formatDateTime(text, DATETIME_FORMAT)
            }];
        if(!this.props.isDealerUser)
            colums.splice(0, 0, ...[
                {
                    title: this.props.intl.formatMessage({
                        id: 'hisTablePanel.column.dealerCode',
                        defaultMessage: '总代编号'
                    }),
                    dataIndex: 'dealerCode',
                    sorter: true,
                    sortOrder: this.props.sortedColumnKey === 'dealerCode' && this.props.sortedOrder,
                }, {
                    title: this.props.intl.formatMessage({
                        id: 'hisTablePanel.column.dealerName',
                        defaultMessage: '总代名称'
                    }),
                    dataIndex: 'dealerName'
                }
            ]);
        const pagination = {
            total: this.props.total,
            pageSize: this.props.pageSize,
            current: this.props.pageIndex + 1,
            ...PAGINATION_OPTIONS
        };
        const locale = {
            emptyText: this.props.errorMessage ? COMMON_TABLE_QUERY_FAIL_TEXT : COMMON_TABLE_EMPTY_TEXT
        };
        return (
            <div>
                <Table
                    className="white-space-nowrap"
                    columns={colums}
                    dataSource={this.props.list}
                    onChange={this.onChangeSort}
                    rowKey="id"
                    pagination={pagination}
                    loading={this.props.loading || this.state.loading}
                    locale={locale}
                    {...TABLE} />
            </div>
        );
    }
}

HisTablePanel.propTypes = {
    errorMessage: PropTypes.string.isRequired,
    list: PropTypes.array.isRequired,
    loading: PropTypes.bool.isRequired,
    total: PropTypes.number.isRequired,
    onChangeSort: PropTypes.func.isRequired,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
    pageIndex: PropTypes.number,
    pageSize: PropTypes.number,
    sortedColumnKey: PropTypes.string,
    sortedOrder: PropTypes.string,
};

import {connect} from 'react-redux';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {tableSearch} from './actions';

const getData = selectorFactory(['page', 'domainData', 'listHis', 'data']);
const mapStateToProps = state => ({
    list: getData(state),
    total: state.getIn(['page', 'domainData', 'listHis', 'total']),
    pageIndex: state.getIn(['page', 'appState', 'queryBySortHis', 'pageIndex']),
    pageSize: state.getIn(['page', 'appState', 'queryBySortHis', 'pageSize']),
    loading: state.getIn(['page', 'domainData', 'listHis', 'isFetching']),
    errorMessage: state.getIn(['page', 'domainData', 'listHis', 'message']),
    sortedColumnKey: state.getIn(['page', 'appState', 'queryBySortHis', 'sortedColumnKey']),
    sortedOrder: state.getIn(['page', 'appState', 'queryBySortHis', 'sortedOrder']),
    isDealerUser: state.getIn((['page', 'domainData', 'initData', 'isDealerUser'])),
});

const mapDispatchToProps = dispatch => ({
    onChangeSort: option => dispatch(tableSearch(option))
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(HisTablePanel));
